package de.koller.faculae;

import de.koller.aframe.plain.ADisplayPlain;
import de.koller.aframe.plain.IARootPlain;
import de.koller.aframe.util.ALoop;
import de.koller.aframe.util.thread.ICommand;
import de.koller.faculae.engine.FEngine;
import de.koller.faculae.input.FInput;
import de.koller.faculae.render.FRender;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Sea3X;
import java.awt.Graphics2D;

public class FRoot extends Sea3X implements IARootPlain {
	
	private final ALoop loop = new ALoop( this );
	public final ADisplayPlain display = new ADisplayPlain( this, 800, 600, "Faculae" );
	
	@Child public final FInput input = new FInput();
	@Child public final FRender render = new FRender();
	@Child public final FEngine engine = new FEngine();
	private long time = 0;
	
	@Override
	protected void init() {
		super.init();
		
		this.setIgnoreChildRender( true );
		display.init();
	}

	@Override
	protected void release() {
		super.release();
		
		display.release();
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		time += elapsedTime;
	}
	
	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		display.render();
	}
	
	public void start() {
		loop.begin();
	}
	
	public void stop() {
		loop.stop();
	}

	public void call( ICommand command ) {
		loop.thread.call( command );
	}

	@Override
	public long getSystemTime() {
		return System.currentTimeMillis();
	}

	public long getGameTime() {
		return time;
	}

	@Override
	public void displayClosed() {
		this.stop();
	}

	@Override
	public void render( Graphics2D g ) {
		render.renderThis( g );
	}
	
}
